package com.ycxh.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * mvc配置的支持类
 * https://blog.csdn.net/WindMo356/article/details/108074918
 * “WebMvcConfigurerAdapter is deprecated”解决方法 https://www.cnblogs.com/shenyuanfeng/archive/2020/08/06/13449804.html
 */
@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {

    @Autowired
    private AuthInterceptor authInterceptor;

    // 允许跨域的源
    private static String origins = "*";

    /**
     * 注册拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor).addPathPatterns("/**")
                // 所有请求都要拦截，因为要判断ip
                // .excludePathPatterns(
                //         "/", "/login", "/loginJQ", "/user/login"
                //         , "/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg"
                //         , "/**/*.jpeg", "/**/*.gif", "/**/fonts/*", "/**/*.svg")
        ;
        super.addInterceptors(registry);
    }


    /**
     * 页面跨域访问Controller过滤
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 设置允许跨域的路径
                .allowedHeaders("*")
                .allowedMethods("*") // 设置允许的方法"GET", "POST", "PUT", "DELETE"
                .allowedOrigins(origins); // 设置允许跨域请求的域名
    }

    /**
     * 解决接口返回中文乱码问题
     * https://blog.csdn.net/m0_67505608/article/details/123471602
     * https://blog.csdn.net/qq_32048567/article/details/120536410
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        for (HttpMessageConverter<?> converter : converters) {
            // 解决 Controller 返回普通文本中文乱码问题
            if (converter instanceof StringHttpMessageConverter) {
                ((StringHttpMessageConverter) converter).setDefaultCharset(StandardCharsets.UTF_8);
            }

            // 解决 Controller 返回json对象中文乱码问题
            if (converter instanceof MappingJackson2HttpMessageConverter) {
                ((MappingJackson2HttpMessageConverter) converter).setDefaultCharset(StandardCharsets.UTF_8);
            }
        }
    }

    /**
     * 访问resources\static目录下的js、css等静态资源
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/resources/")
                // tomcat临时目录 C:\Users\用户\AppData\Local\Temp\tomcat-docbase.1605834731612754802.8089\
                // 每次重启不一样 且linux上长期未访问会被清理掉
                .addResourceLocations("file:"+ getServletContext().getRealPath("/"));
        super.addResourceHandlers(registry);
    }

    /**
     * 注册视图控制器
     **/
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/error400Page").setViewName("error/400");
        registry.addViewController("/error401Page").setViewName("error/401");
        registry.addViewController("/error404Page").setViewName("error/404");
        registry.addViewController("/error500Page").setViewName("error/500");
    }

}
